/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author WenYong
 */
@ManagedBean
@RequestScoped
public class StakeholderAddUnstructuredContentUploadVideoManager {

    private String videoUrl;
    private FacesContext ctx = FacesContext.getCurrentInstance();

    /** Creates a new instance of StakeholderAddUnstructuredContentUploadVideoManager */
    public StakeholderAddUnstructuredContentUploadVideoManager() {
    }

    public void handleVideoUpload(FileUploadEvent event) throws AbortProcessingException, NoSuchAlgorithmException {
        try {
            String panel = "";
            if (ctx.getExternalContext().getSessionMap().containsKey("templatePanels")) {
                panel = ctx.getExternalContext().getSessionMap().get("Panel").toString();
            }
            String fileName = panel + "_" + event.getFile().getFileName();
            String filePath =
                    ctx.getExternalContext().getRealPath("images/stakeholder/unstructured/video");
            System.out.println("filePath is: " + filePath);
            File result = new File(filePath + System.getProperty("file.separator") + fileName);
            FileOutputStream out = new FileOutputStream(result);

            int a;
            int BUFFER_SIZE = 8192;
            byte[] buffer = new byte[BUFFER_SIZE];

            InputStream is = event.getFile().getInputstream();

            while (true) {
                a = is.read(buffer);

                if (a < 0) {
                    break;
                }

                out.write(buffer, 0, a);
                out.flush();
            }

            out.close();
            is.close();

            setVideoUrl("images/stakeholder/unstructured/" + fileName);
            System.out.println("imagefilepath: " + getVideoUrl());
            FacesMessage msg = new FacesMessage("Succesful", fileName + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            ctx.getExternalContext().getSessionMap().put("videoName", fileName);
            System.out.println("Image Name Session: " + fileName);
        } catch (IOException e) {
            FacesContext.getCurrentInstance().addMessage("sPortalform", new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "File upload failed with I/O error.", null));
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String getVideoUrl() {
        return videoUrl;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }

    public void confirmVideo() {
        ctx.getExternalContext().getSessionMap().put("videoName", videoUrl);
        String template = "";
        if (ctx.getExternalContext().getSessionMap().containsKey("TemplateSelected")) {
            template = ctx.getExternalContext().getSessionMap().get("TemplateSelected").toString();
            System.out.println("Templatte: " + template);
        }
        try {
            String serverName = ctx.getExternalContext().getRequestServerName();
            String serverPort = "8080";
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            if (template.equalsIgnoreCase("1")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateOne.xhtml");
            } else if (template.equalsIgnoreCase("2")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateTwo.xhtml");
            } else if (template.equalsIgnoreCase("3")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateThree.xhtml");
            } else if (template.equalsIgnoreCase("4")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateFour.xhtml");
            } else if (template.equalsIgnoreCase("5")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateFive.xhtml");
            } else if (template.equalsIgnoreCase("6")) {
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentTemplateSix.xhtml");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void videoAdd() {
        String domain = "http://www.youtube.com/v/";
        int posOfAnd = videoUrl.indexOf("&");
        int equal = videoUrl.indexOf("=");
        equal = equal + 1;
        videoUrl = videoUrl.substring(equal, posOfAnd);
        videoUrl = domain + videoUrl;
    }
}
